import db from './createDb'
import Mock from 'mockjs'

// 读取分类列表信息
Mock.mock('productcate/list', 'get', () => {
  const result = db.select('zy_productcate')
  return { err: 0, result, desc: '查询成功' }
})

// 读取通过审核的分类名
Mock.mock('productcate/classify', 'get', () => {
  const result = db.select('zy_productcate', ['id', 'title', 'orderid'], item => item.state === 2).sort((a, b) => a.orderid - b.orderid)
  return { err: 0, result, desc: '查询成功' }
})

// 添加分类
Mock.mock('productcate/add', 'post', ({ body }) => {
  const { title, picture, orderid, state = 1 } = JSON.parse(body)
  db.add('zy_productcate', { title, picture, orderid, state })
  return {
    err: 0,
    desc: '添加成功'
  }
})

// 修改分类
Mock.mock('productcate/updata', 'post', ({ body }) => {
  const { id, title, picture, orderid, state } = JSON.parse(body)
  db.put('zy_productcate', id, { title, picture, orderid, state })
  return {
    err: 0,
    desc: '修改成功'
  }
})

// 修改state状态
Mock.mock('productcate/state', 'post', ({ body }) => {
  const { id, state } = JSON.parse(body)
  db.put('zy_productcate', id, { state })
  return {
    err: 0,
    desc: '状态修改成功'
  }
})

// 删除分类
Mock.mock('productcate/delete', 'post', ({ body }) => {
  const { ids } = JSON.parse(body)
  db.delete('zy_productcate', ids)
  return {
    err: 0,
    desc: '删除成功'
  }
})
